package com.tree.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.department.DepartmentDTO;
import com.tree.backend.entity.dto.department.DepartmentQueryDTO;
import com.tree.backend.entity.vo.department.DepartmentTreeVO;
import com.tree.backend.entity.vo.department.DepartmentVO;
import com.tree.backend.service.IDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门组织表 前端控制器
 * </p>
 */
@RestController
@RequestMapping({"/department", "/departments"})
@RequiredArgsConstructor
public class DepartmentController {

    private final IDepartmentService departmentService;

    /**
     * 获取部门列表（分页）
     */
    @GetMapping("/list")
    public Result<IPage<DepartmentVO>> getDepartmentList(DepartmentQueryDTO queryDTO) {
        IPage<DepartmentVO> departmentList = departmentService.getDepartmentList(queryDTO);
        return Result.success(departmentList);
    }

    /**
     * 获取部门详情
     */
    @GetMapping("/{id}")
    public Result<DepartmentVO> getDepartmentDetail(@PathVariable Long id) {
        DepartmentVO departmentVO = departmentService.getDepartmentDetail(id);
        if (departmentVO == null) {
            return Result.error(404, "部门不存在");
        }
        return Result.success(departmentVO);
    }

    /**
     * 新增部门
     */
    @PostMapping("")
    public Result<DepartmentVO> addDepartment(@RequestBody DepartmentDTO departmentDTO) {
        try {
            DepartmentVO departmentVO = departmentService.addDepartment(departmentDTO);
            return Result.success(departmentVO);
        } catch (RuntimeException e) {
            return Result.error(400, e.getMessage());
        }
    }

    /**
     * 更新部门
     */
    @PutMapping("/{id}")
    public Result<DepartmentVO> updateDepartment(@PathVariable Long id, @RequestBody DepartmentDTO departmentDTO) {
        DepartmentVO departmentVO = departmentService.updateDepartment(id, departmentDTO);
        if (departmentVO == null) {
            return Result.error(404, "部门不存在");
        }
        return Result.success(departmentVO);
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDepartment(@PathVariable Long id) {
        boolean success = departmentService.deleteDepartment(id);
        if (!success) {
            return Result.error(400, "删除失败，该部门可能存在子部门");
        }
        return Result.success(true);
    }

    /**
     * 获取部门树
     */
    @GetMapping("/tree")
    public Result<List<DepartmentTreeVO>> getDepartmentTree() {
        List<DepartmentTreeVO> departmentTree = departmentService.getDepartmentTree();
        return Result.success(departmentTree);
    }
    
    /**
     * 获取所有部门列表（不分页）
     */
    @GetMapping("")
    public Result<List<DepartmentVO>> getAllDepartments() {
        List<DepartmentVO> departments = departmentService.getAllDepartments();
        return Result.success(departments);
    }
}
